package com.epfresh.api.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.aigestudio.wheelpicker.core.AbstractWheelPicker;
import com.aigestudio.wheelpicker.core.IWheelPicker;
import com.aigestudio.wheelpicker.view.WheelStraightPicker;
import com.epfresh.supplier.R;

import java.util.List;

public class PackagePriceDialog extends Dialog implements View.OnClickListener {
    private View root;
    private ViewGroup container;
    private Button rButton,lButton;
    private IWheelPicker picker;

    private int index1;

    private String data1;


    private WheelStraightPicker wheelUnit1;


    private OnPositiveClickListener onPositiveClickListener;
    private Context mContext;
    public PackagePriceDialog(Context context) {
        super(context);
        mContext = context;
        padding = mContext.getResources().getDimensionPixelSize(R.dimen.WheelPadding);
        textSize = mContext.getResources().getDimensionPixelSize(R.dimen.TextSizeLarge);
        itemSpace = mContext.getResources().getDimensionPixelSize(R.dimen.ItemSpaceLarge);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(true);
//        setOnDismissListener(new OnDismissListener() {
//            @Override
//            public void onDismiss(DialogInterface dialog) {
//                container.removeAllViews();
//            }
//        });

        root = getLayoutInflater().inflate(R.layout.dialog_new_layout, null);
        container = (ViewGroup) root.findViewById(R.id.content);
        container.removeAllViews();
        rButton = (Button) root.findViewById(R.id.positiveButton);
        rButton.setOnClickListener(this);
        lButton = (Button) root.findViewById(R.id.negativeButton);
        lButton.setOnClickListener(this);


        setTitle("选择包装");
        View v = LayoutInflater.from(getContext()).inflate(R.layout.dialog_price_package,null);
        setContentView(v);

        wheelUnit1 = (WheelStraightPicker) findViewById(R.id.wheel_unit_1);


    }

    public void setData(List<String> units){

        if (units != null && units.size()>0 ) {

            wheelUnit1.setData(units);
            wheelUnit1.setOnWheelChangeListener(new AbstractWheelPicker.OnWheelChangeListener() {
                @Override
                public void onWheelScrolling(float deltaX, float deltaY) {

                }

                @Override
                public void onWheelSelected(int index, String data) {
                    data1 = data;
                    index1 = index;
                }

                @Override
                public void onWheelScrollStateChanged(int state) {

                }
            });

        }
    }


    @Override
    public void setContentView(View view) {
        container.addView(view);
        super.setContentView(root);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.positiveButton:

                if (isPositiveDismiss)
                dismiss();
                if (onPositiveClickListener != null){
                    onPositiveClickListener.onPositiveClickListener(index1,data1);
                }
                break;
            case R.id.negativeButton:
                dismiss();
                break;
        }
    }


    public OnPositiveClickListener getOnPositiveClickListener() {
        return onPositiveClickListener;
    }

    public void setOnPositiveClickListener(OnPositiveClickListener onPositiveClickListener) {
        this.onPositiveClickListener = onPositiveClickListener;
    }

    public void setTitle(String title){
        ((TextView)root.findViewById(R.id.title)).setText(title);
    }
    public void setLeftText(String text){
        ((TextView)root.findViewById(R.id.negativeButton)).setText(text);
    }
    public void setRightText(String text){
        ((TextView)root.findViewById(R.id.positiveButton)).setText(text);
    }

    public boolean isPositiveDismiss() {
        return isPositiveDismiss;
    }

    public void setPositiveDismiss(boolean positiveDismiss) {
        isPositiveDismiss = positiveDismiss;
    }

    public void setCurrentItem(int index1){
        wheelUnit1.setItemIndex(index1);
    }

    private boolean isPositiveDismiss;



    private int padding;
    private int textSize;
    private int itemSpace;


    public interface OnPositiveClickListener{
        public void onPositiveClickListener(int index1, String data1);
    }
}